#include "app_common.h"
#include <stdlib.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>


long long app_common_getCurrentTime(void)
{
    struct timeval tv;
    //获取当前时间
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

char *app_common_charsToHexstr(char *chars, int chars_len)
{
    // abcd -> 61 62 63 64
    //给hexstr分配内存
    char *hex_str = (char *)malloc(chars_len * 2 + 1);
    for (int i = 0; i < chars_len; i++)
    {
        sprintf(hex_str + i * 2, "%02x", chars[i]);
    }

    //添加结束符
    hex_str[chars_len * 2] = '\0';
    return hex_str;
    
}

char *app_common_hexstrToChars(char *hexstr, int *chars_len)
{
    // 61 62 63 64 -> abcd
    //给chars分配内存
    int hexstr_len = strlen(hexstr);
    *chars_len = hexstr_len / 2;
    char *chars = (char *)malloc(*chars_len);

    for( int i = 0 ; i < hexstr_len ; i += 2 )
    {
        sscanf( hexstr + i , "%2hhx" , chars + i / 2 );
    }

    return chars;
}


